from presenter import basepresenter
from domain import discussion

class EditDiscussionPresenter(basepresenter.BasePresenter):

    def respondToGet(self):
        # get the discussion key
        if (self.request.get('key')):
            editdiscussion = self.discussiontask.getDiscussionByKey(self.request.get('key'))
            self.template_values['discussion'] = editdiscussion
        else:
            editdiscussion = None

        # set up the page title
        page_title = self.sitetitle + ' - Add / Edit Discussion'
        self.template_values['page_title'] = page_title

        #get the view to draw the page
        self.view.showGetResponse(self.template_values)

    def respondToPost(self):
        if (self.request.get('action') == "Delete"):
            olddiscussion = self.discussiontask.getDiscussionByKey(self.request.get('discussionkey'))
            self.discussiontask.deleteDiscussion(olddiscussion)
        else:
            # populate a discussion object from the request
            newdiscussion = discussion.Discussion()
            if (self.request.get('discussionkey')):
                newdiscussion = self.discussiontask.getDiscussionByKey(self.request.get('discussionkey'))
            newdiscussion.title = self.request.get('title')
            newdiscussion.description = self.request.get('description')
            imageurl = self.request.get('imagelink')
            if (imageurl):
                newdiscussion.imagelink = self.request.get('imagelink')
            newdiscussion.type = self.request.get('type')
            if (self.request.get('enabled')):
                newdiscussion.enabled = True
            else:
                newdiscussion.enabled = False

            # create the task and save the article
            self.discussiontask.saveDiscussion(newdiscussion)
        #get the view to draw the page
        self.view.showPostResponse(self.template_values)